﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using QLNHK.BLL;
using Infragistics.Win.UltraWinGrid;


namespace QLNHK
{
    public partial class frmBaoCaoDanhSachNhanKhauChuyenDiNoiKhac : Form
    {
        private DataSet dsData = new DataSet();
        frmWait frmWaiting;

        public frmBaoCaoDanhSachNhanKhauChuyenDiNoiKhac()
        {
            InitializeComponent();
        }
        private void frmBaoCaoDanhSachNhanKhauChuyenDiNoiKhac_Load(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                Global.setDataToComboThanhPho(cboThanhPho);
                cboThanhPho.Value = ConfigNHK.Install.ThanhPho;
                dtTuNgay.Value = dtDenNgay.Value = System.DateTime.Today;
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
                MessageBox.Show(ex.Message);
            }
            finally { Cursor = Cursors.Default; }
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            frmWaiting = new frmWait();
            ThreadPool.QueueUserWorkItem(OnProcess);
            frmWaiting.ShowDialog(this);
        }
        private void OnProcess(object o)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    frmWaiting.SetCaption("Vui lòng chờ giây lát");

                    //Load data tại đây
                    this.LoadData(ref frmWaiting);
                    frmWaiting.DialogResult = DialogResult.OK;
                }));
            }
        }
        private void LoadData(ref frmWait frmW)
        {
            try
            {
                //Load data
                dsData = BaoCao.DanhSachNhanKhauChuyenDiNoiKhac(dtTuNgay.DateTime, dtDenNgay.DateTime, System.Convert.ToInt32(cboThanhPho.Value), System.Convert.ToInt32(cboQuanHuyen.Value), System.Convert.ToInt32(cboXaPhuong.Value), System.Convert.ToInt32(cboToDanPho.Value));
                gridDanhSachNhanKhau.DataSource = dsData.Tables[0];

                int tongSoNhanKhau = dsData.Tables[0].Rows.Count;
                int tongNam = dsData.Tables[0].Select("GioiTinh = 1").Length;
                int tongNu = dsData.Tables[0].Select("GioiTinh = 0").Length;
                lblTongSo.Text = string.Format("Tổng số nhân khẩu: {0} - Nam: {1}, Nữ: {2}", tongSoNhanKhau, tongNam, tongNu);
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void cboThanhPho_ValueChanged(object sender, EventArgs e)
        {
            if (cboThanhPho.Value != null)
            {
                int cboThanhPhoValue = 0;
                Int32.TryParse(cboThanhPho.Value.ToString(), out cboThanhPhoValue);
                if (cboThanhPhoValue > 0)
                    Global.setDataToComboQuanHuyen(cboQuanHuyen, Convert.ToInt32(cboThanhPho.Value));
            }
        }
        private void cboQuanHuyen_ValueChanged(object sender, EventArgs e)
        {
            if (cboQuanHuyen.Value != null)
            {
                int cboQuanHuyenValue = 0;
                Int32.TryParse(cboQuanHuyen.Value.ToString(), out cboQuanHuyenValue);
                if (cboQuanHuyenValue > 0)
                    Global.setDataToComboXaPhuong(cboXaPhuong, Convert.ToInt32(cboQuanHuyen.Value));
            }
        }
        private void cboXaPhuong_ValueChanged(object sender, EventArgs e)
        {
            if (cboXaPhuong.Value != null)
            {
                int cboXaPhuongValue = 0;
                Int32.TryParse(cboXaPhuong.Value.ToString(), out cboXaPhuongValue);
                if (cboXaPhuongValue > 0)
                    Global.setDataToComboToDanPho(cboToDanPho, Convert.ToInt32(cboXaPhuong.Value));
            }
        }

        private void gridDanhSachNhanKhau_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            try
            {
                e.Layout.Override.CellClickAction = CellClickAction.RowSelect;
                e.Layout.Override.HeaderAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
                e.Layout.Override.WrapHeaderText = Infragistics.Win.DefaultableBoolean.True;

                //Comment by setting on design form
                //e.Layout.ViewStyleBand = ViewStyleBand.OutlookGroupBy;
                //e.Layout.Override.AllowGroupBy = Infragistics.Win.DefaultableBoolean.True;
                //e.Layout.Override.GroupByColumnsHidden = Infragistics.Win.DefaultableBoolean.False;

                UltraGridBand band = e.Layout.Bands[0];

                //Hidden columns
                //band.Columns["ID"].Hidden = true;

                //Caption
                band.Columns["STT"].Header.Caption = "STT";
                band.Columns["HoTen"].Header.Caption = "Họ và tên";
                band.Columns["GioiTinh"].Header.Caption = "Giới tính";
                band.Columns["NgaySinh"].Header.Caption = "Ngày sinh";
                band.Columns["NgayChuyenDi"].Header.Caption = "Ngày chuyển đi";
                band.Columns["NoiChuyenDen"].Header.Caption = "Nơi chuyển đến";
                band.Columns["ThuongTruDiaChi"].Header.Caption = "Địa chỉ thường trú";
                band.Columns["ThuongTruTo"].Header.Caption = "Tổ dân phố";
                band.Columns["ThuongTruXaPhuong"].Header.Caption = "Xã/ Phường";
                band.Columns["ThuongTruQuanHuyen"].Header.Caption = "Quận/ Huyện";
                band.Columns["ThuongTruTinhTP"].Header.Caption = "Tỉnh/ Thành phố";
                band.Columns["GhiChu"].Header.Caption = "Ghi chú";

                //Set position
                band.Columns["STT"].Header.VisiblePosition = 0;
                band.Columns["HoTen"].Header.VisiblePosition = 1;
                band.Columns["NgaySinh"].Header.VisiblePosition = 2;
                band.Columns["GioiTinh"].Header.VisiblePosition = 3;
                band.Columns["NgayChuyenDi"].Header.VisiblePosition = 4;
                band.Columns["NoiChuyenDen"].Header.VisiblePosition = 5;
                band.Columns["ThuongTruDiaChi"].Header.VisiblePosition = 6;
                band.Columns["ThuongTruTo"].Header.VisiblePosition = 7;
                band.Columns["ThuongTruXaPhuong"].Header.VisiblePosition = 8;
                band.Columns["ThuongTruQuanHuyen"].Header.VisiblePosition = 9;
                band.Columns["ThuongTruTinhTP"].Header.VisiblePosition = 10;
                band.Columns["GhiChu"].Header.VisiblePosition = 11;

                //Format
                band.Columns["NgaySinh"].MaskInput = band.Columns["NgayChuyenDi"].MaskInput = "{LOC}dd/mm/yyyy";
                band.Columns["STT"].CellAppearance.TextHAlign =
                band.Columns["GioiTinh"].CellAppearance.TextHAlign =
                band.Columns["NgaySinh"].CellAppearance.TextHAlign =
                band.Columns["NgayChuyenDi"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
                band.Columns["HoTen"].Width = 150;

                //Editable
                band.Columns["NgaySinh"].CellActivation = band.Columns["NgayChuyenDi"].CellActivation = Activation.NoEdit;
                band.Columns["GioiTinh"].ValueList = Global.getValueListGioiTinh();
                band.Columns["GioiTinh"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDownList;
                band.Columns["GioiTinh"].CellActivation = Activation.NoEdit;
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }

        private void btnXuatExcel_Click(object sender, EventArgs e)
        {
            XuatExcel("Danh sach nhan khau chuyen di noi khac", saveFileDialog1, excelExporter, Infragistics.Excel.WorkbookFormat.Excel97To2003, gridDanhSachNhanKhau);
        }
        private void XuatExcel(
            string fileName,
            SaveFileDialog saveDialog,
            Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter exporter,
            Infragistics.Excel.WorkbookFormat excelType,
            Infragistics.Win.UltraWinGrid.UltraGrid grid)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                saveDialog.FileName = fileName;
                saveDialog.Filter = "Excel files (*.xls, *.xlxs)|*.xls;*.xlxs";
                if (DialogResult.OK == saveDialog.ShowDialog())
                {
                    if (saveDialog.FileName.Length > 0)
                    {
                        excelExporter.Export(grid, saveDialog.FileName, excelType);

                        if (DialogResult.OK == MessageBox.Show("Bạn có muốn mở tệp tin Excel được xuất ra không?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                        {
                            System.Diagnostics.Process.Start(saveDialog.FileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
                MessageBox.Show(ex.Message);
            }
            finally { Cursor = Cursors.Default; }
        }
    }
}
